package cn.com.api.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;












import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiNewFindOrder;

import cn.com.api.entity.ApiT_order;
import cn.com.api.requestpage.OrderConfirmPage;
import cn.com.api.service.ApiCarService;
import cn.com.api.service.ApiNewFindOrderService;
import cn.com.api.service.ApiOrderService;
import cn.com.api.service.ApiT_orderService;
import cn.com.api.service.ApiT_order_detailService;
import cn.com.api.service.CheckService;
import cn.com.util.JsonUtil;
import cn.com.util.Utils;


@Controller
@RequestMapping(Utils.SYS_API + "newfindOrder/")
public class ApiNewFindOrderController {
	
	/**
	 * 日志工具
	 */
	private static Logger logger = Logger.getLogger(ApiNewFindOrderController.class);
	
	@Autowired
	private CheckService checkService;
	
	@Autowired
	private ApiOrderService apiOrderService;
	
	@Autowired
	private ApiCarService apiCarService;

	@Autowired
	private ApiNewFindOrderService apiNewFindOrderService;
	 
	@Autowired
	private ApiT_order_detailService apiT_order_detailService;
	
	@Autowired
	private ApiT_orderService apiT_orderService;
	
	/**
	 * 立即下单接口
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "newfindNowOrder")
	public void confirmNowOrder(HttpServletRequest request, HttpServletResponse response, 
			String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {			

			//String user_id = request.getParameter("user_id");//用户ID
			String name = request.getParameter("name");//姓名
			String phone = request.getParameter("phone");//电话
			String money = request.getParameter("money");//金额
			String desc = request.getParameter("desc");//留言
			String pro_id = request.getParameter("pro_id");//产品ID
			String goods_id = request.getParameter("discover_id");//商品ID就是活动ID
			String shop_id = request.getParameter("shop_id");//店铺ID
			String num = request.getParameter("personNum");//商品数量
			String discover_id = request.getParameter("discover_id");//活动ID
			String title = request.getParameter("title");//活动ID
			String ordernum = Utils.getDingDanNo("");//订单号
			String price = request.getParameter("price");
			
			logger.info("========[ApiNewFindOrderController[confirmNowOrder[[u_id:"+user_id+",name:"+name+",phone:"+phone+",money:"+money+",desc:"+desc+",pro_id:"+pro_id+",goods_id:"+goods_id+",shop_id:"+shop_id+",num:"+num+",discover_id:"+discover_id+",ordernum:"+ordernum+"]]============");
			
			ApiNewFindOrder apiNewFindOrder = new ApiNewFindOrder();
			apiNewFindOrder.setUser_id(user_id);
			apiNewFindOrder.setMobile(phone);									
			apiNewFindOrder.setMoney(new BigDecimal(money));			
			apiNewFindOrder.setPrice(new BigDecimal(price));
			apiNewFindOrder.setName(name);
			apiNewFindOrder.setDiscover_id(discover_id);
			apiNewFindOrder.setGoods_id(goods_id);
			apiNewFindOrder.setProduct_id(pro_id);
			apiNewFindOrder.setNum(Integer.parseInt(num));
			apiNewFindOrder.setBz(desc);
			apiNewFindOrder.setTitles(title);
			apiNewFindOrder.setOrdernum(ordernum);
			apiNewFindOrder.setShop_id(shop_id);
			apiNewFindOrder.setZi_status(ApiNewFindOrder.ZI_STATUS_START);
			apiNewFindOrder.setCurDate(new Date());
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiNewFindOrderService.initOrder(apiNewFindOrder);
			}

		} catch (Exception e) {
			logger.error("========[ApiNewFindOrderController[confirmNowOrder]]错误=======", e);
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	



	/**
	 * 确认订单接口
	 * @param orderConfirmPage
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "newfindConfirmOrder")
	public void confirmOrder(OrderConfirmPage orderConfirmPage, 
			HttpServletRequest request, HttpServletResponse response, String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//String user_id = request.getParameter("user_id");//用户ID
			String name = request.getParameter("name");//姓名
			String phone = request.getParameter("phone");//电话
			String money = request.getParameter("money");//金额
			String desc = request.getParameter("desc");//留言
			String num = request.getParameter("personNum");//商品数量
			String rid = request.getParameter("rid");//订单号		
			
			logger.info("========[ApiNewFindOrderController[confirmOrder[[u_id:"+user_id+",name:"+name+",phone:"+phone+",money:"+money+",desc:"+desc+",num:"+num+",ordernum:"+rid+"]]============");
			
			ApiNewFindOrder apiNewFindOrder = new ApiNewFindOrder();
			apiNewFindOrder.setUser_id(user_id);
			apiNewFindOrder.setMobile(phone);									
			apiNewFindOrder.setMoney(new BigDecimal(money));						
			apiNewFindOrder.setName(name);
			apiNewFindOrder.setNum(Integer.parseInt(num));
			apiNewFindOrder.setBz(desc);
			apiNewFindOrder.setEnable(ApiNewFindOrder.ENABLE_STATUS_SUCCESSS);//5：订单确认已完成
			apiNewFindOrder.setCurDate(new Date());
			apiNewFindOrder.setOrdernum(rid);
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				apiNewFindOrderService.sureOrderInfo(apiNewFindOrder);
			}
			map.put("rid",rid);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "交易成功！");
		} catch (Exception e) {
			logger.error("========[ApiNewFindOrderController[confirmOrder]]错误=======", e);
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, e.getStackTrace());
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	/**
	 * 支付确认接口
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "newfindConfirmPay")
	public void confirmPay(HttpServletRequest request, 
			HttpServletResponse response, String user_id, String token) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String sumPrice = request.getParameter("sumPrice");
			String payType = request.getParameter("payType");
			String ordernum = request.getParameter("rid");
			String num = request.getParameter("personNum");
			
			ApiNewFindOrder apiNewFindOrder = new ApiNewFindOrder();
			apiNewFindOrder.setUser_id(user_id);									
			apiNewFindOrder.setMoney(new BigDecimal(sumPrice));						
			apiNewFindOrder.setNum(Integer.parseInt(num));
			apiNewFindOrder.setOrdernum(ordernum);
			apiNewFindOrder.setZi_status(ApiNewFindOrder.ZI_STATUS_SUCCESSS);
			apiNewFindOrder.setCurDate(new Date());
			apiNewFindOrder.setPayType(payType);
			apiNewFindOrder.setZf_type(payType);
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiNewFindOrderService.confirmOrderPay(apiNewFindOrder);
			}
		}
		catch (Exception e) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, e.getStackTrace());
		}
		
		JsonUtil.printJson(response, JsonUtil.object2json(map));
		
	}
	
	/**
	 * 订单详情接口
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "newfindOrderInfo")
	public void orderInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {		
			String rid = request.getParameter("rid");
			map = apiNewFindOrderService.orderInfo(rid);
		}
		 catch (Exception e) {
				map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
				map.put(Utils.MESSAGE, e.getStackTrace());
			}
		
		JsonUtil.printJson(response, JsonUtil.object2json(map));
		
	}
	
	
	/**
	 * 我的新发现订单列表
	 * 
	 * @author JA
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("find_newFindOrder")
	public void findOrderByUserId(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> param = new HashMap<String, Object>();
		try {
			String user_id = request.getParameter("user_id");
            String status = request.getParameter("status");
            String ordertype = request.getParameter("order_type");
            param.put("user_id", user_id);
            param.put("status", status);
            param.put("ordertype", ordertype);
         			
			List<ApiT_order> list = apiNewFindOrderService.getOrdersList(param);
			List<Map<String, Object>> orderMapList = new ArrayList<>();
			Map<String, Object> orderMap = null;
			for (ApiT_order apiT_order : list) {
				orderMap = new HashMap<>();
				orderMap.put("orderNum", apiT_order.getOrdernum());
	
				orderMap.put("c_avatar", apiT_order.getC_avatar());
				orderMap.put("c_nick", apiT_order.getC_nick());
				orderMap.put("title", apiT_order.getTitles());
				orderMap.put("money", apiT_order.getMoney());
				orderMap.put("price", apiT_order.getMoney());
				orderMap.put("name", apiT_order.getName());
				
				orderMap.put("price_name", apiT_order.getPrice_name());
				orderMap.put("discover_id", apiT_order.getTarget_id());
				orderMap.put("type", apiT_order.getType());
				orderMap.put("cate_type", apiT_order.getCate_type());
				orderMap.put("head_img", apiT_order.getHead_img());
				orderMap.put("product_id", apiT_order.getProduct_id());
				orderMap.put("num", apiT_order.getNum());
				switch (apiT_order.getDd_status()) {
				case 1:// 待付款
					
					orderMap.put("orderStatus", "待支付");
					
					orderMap.put("orderStatusId", 1);
					break;
				case 2:// 已支付
					orderMap.put("orderStatus", "代发货");
					orderMap.put("orderStatusId", 2);
					break;
				case 3:// 已发货
					orderMap.put("orderStatus", "代收货");
					orderMap.put("orderStatusId", 3);
					break;
				case 4:// 已确认收货
					orderMap.put("orderStatus", "已完成");
					orderMap.put("orderStatusId", 4);
					break;
				case 5:// 已取消
					orderMap.put("orderStatus", "已取消");
					orderMap.put("orderStatusId", 5);
					break;
				case 6:// 退货中
					orderMap.put("orderStatus", "退货中");
					orderMap.put("orderStatusId", 6);
					break;
				case 7:// 退货成功
					orderMap.put("orderStatus", "退货失败");
					orderMap.put("orderStatusId", 7);
					break;
				case 8:// 退货失败
					orderMap.put("orderStatus", "退货失败");
					orderMap.put("orderStatusId", 8);
					break;
				default:
					break;
				}
				orderMapList.add(orderMap);
			}
			map.put("datas", orderMapList);
			
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	

}
